package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bean.Account;
import model.bo.ManageAccountBO;

public class ManageAccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    public ManageAccountServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String action = (String) request.getParameter("action");
		ManageAccountBO manageAccountBO = new ManageAccountBO();
		String message = "";
		
		if ("getListAccount".equals(action)) {
			
		    String code = (String) request.getParameter("code");
		    int partID = 0;
		    try{
		        partID = Integer.parseInt(code);
		    }catch (Exception e) {
		        
            }
			ArrayList<Account> listAccount = new ArrayList<Account>();
			listAccount = manageAccountBO.getListAccount(partID);
			RequestDispatcher rd = request.getRequestDispatcher("administrator/listaccount.jsp");
			request.setAttribute("listAccount", listAccount);
			rd.forward(request, response);
			
		} else if("addNewAccount".equals(action)) {
			
			String	accountName = (String) request.getParameter("accountName");
			String  password = (String) request.getParameter("password");;
			int privilege = Integer.parseInt((String) request.getParameter("privilege"));
			String fullName = (String) request.getParameter("fullName");;
			String dob = (String) request.getParameter("dob");
			String phone = (String) request.getParameter("phone");
			String email = (String) request.getParameter("email");
			boolean gender = Boolean.parseBoolean((String) request.getParameter("gender"));
			
			Account acc = new Account();
			acc.setNameAccount(accountName);
			acc.setPassword(password);
			acc.setPrivilege(privilege);
			acc.setName(fullName);
			acc.setDateOfBirth(dob);
			acc.setPhoneNumber(phone);
			acc.setEmail(email);
			acc.setSex(gender);
			
			if(manageAccountBO.addNewAccount(acc) > 0){
				request.setAttribute("message", "Add a new account is successfully");
			}else{
				request.setAttribute("message", "Add a new account was been an error");
			}
			RequestDispatcher rd = request.getRequestDispatcher("administrator/addnewaccount.jsp");
			rd.forward(request, response);
			
		} else if("editAccount".equals(action)) {  // Update information of account have been login
			
		    String accountName = request.getParameter("ID");
		    Account account = manageAccountBO.getAccount(accountName);
		    RequestDispatcher rd = request.getRequestDispatcher("administrator/editaccount.jsp");
		    request.setAttribute("acc", account);
		    rd.forward(request, response);
		    
		} else if("updateAccount".equals(action)) {
		    
		    String    accountName = (String) request.getParameter("accountName");
            String  password = (String) request.getParameter("password");;
            int privilege = Integer.parseInt((String) request.getParameter("privilege"));
            String fullName = (String) request.getParameter("fullName");;
            String dob = (String) request.getParameter("dob");
            String phone = (String) request.getParameter("phone");
            String email = (String) request.getParameter("email");
            boolean gender = Boolean.parseBoolean((String) request.getParameter("gender"));
            
            Account acc = new Account();
            acc.setNameAccount(accountName);
            acc.setPassword(password);
            acc.setPrivilege(privilege);
            acc.setName(fullName);
            acc.setDateOfBirth(dob);
            acc.setPhoneNumber(phone);
            acc.setEmail(email);
            acc.setSex(gender);
            
            if(manageAccountBO.updateAccount(acc) > 0){
                
                request.setAttribute("message", "Update information account successfully!!! ");
            }else{
                request.setAttribute("message", "Update information account was been an error");
            }
            RequestDispatcher rd = request.getRequestDispatcher("ManageAccountServlet?action=getListAccount&code=0");
            rd.forward(request, response);
            
		} else if("deleteAccount".equals(action)) {

			String accountName = (String) request.getParameter("ID");
			if(manageAccountBO.deleteAccount(accountName) > 0) {
				// delete successfully!
				message = "1 row was deleted successfully";
			} else {
				// delete error!
				message = "Erorr when delete account";
			}
			RequestDispatcher rd = request.getRequestDispatcher("ManageAccountServlet?action=getListAccount&code=0");
			request.setAttribute("message", message);
			rd.forward(request, response);
		}
	}

}
